/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Bean;

import Action.CategorySessionBeanLocal;
import Entity.Category;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.naming.InitialContext;

/**
 *
 * @author Cyrax
 */
@ManagedBean
@SessionScoped
public class CategoryBean {

    @EJB
    private CategorySessionBeanLocal categorySessionBean;
    List<Category> ct;
    private Category cat;
    private Category selectCat;
    private String erroMs;

    /** Creates a new instance of CategoryBean */
    public CategoryBean() {
        cat = new Category();
    }

    public List<Category> getListAllCategory() {

        try {
            ct = categorySessionBean.listAllCategory();
//            System.out.println("hello:" + ct);

        } catch (Exception e) {
            System.out.println("Eror list category : " + e.getMessage());
        }
        return ct;
    }

    public void insertCategory() {


        try {

            if (cat.getCategoryName() == null) {
                System.out.println("Category is not null!!!");

            } else {
                categorySessionBean.addCategory(cat);
            }

        } catch (Exception ex) {
            System.out.println("Error : " + ex.getMessage());
        }
    }

    public void updateCategory() {

        int catId = selectCat.getCategoryId();
        categorySessionBean.updateCategory(catId, selectCat.getCategoryName());

    }

    public void deleteCategory(Category cat) {
        try {

            categorySessionBean.deleteCategory(cat);
        } catch (Exception e) {
            System.out.println("Error : " + e.getMessage());
        }
    }
    private ArrayList<SelectItem> selectCategory;

    public ArrayList<SelectItem> getSelectCategory() {
        List<Category> lstCat = getListAllCategory();
        ArrayList<SelectItem> items = new ArrayList<SelectItem>();
        for (Category c : lstCat) {
            items.add(new SelectItem(c.getCategoryId(), c.getCategoryName()));
        }
        return items;
    }

    /**
     * @return the cat
     */
    public Category getCat() {
        return cat;
    }

    /**
     * @param cat the cat to set
     */
    public void setCat(Category cat) {
        this.cat = cat;
    }

    public void update() {
        cat = new Category();
        FacesContext faces = FacesContext.getCurrentInstance();
        Map map = faces.getExternalContext().getRequestParameterMap();

        int idCat = Integer.valueOf((String) map.get("categoryId"));

        String name = (String) map.get("categoryName");
        cat.setCategoryId(idCat);
        cat.setCategoryName(name);
    }

    /**
     * @return the selectCat
     */
    public Category getSelectCat() {
        return selectCat;
    }

    /**
     * @param selectCat the selectCat to set
     */
    public void setSelectCat(Category selectCat) {
        this.selectCat = selectCat;
    }

    public void checkCategoryName() {
        if (categorySessionBean.checkCategoryName(cat.getCategoryName())) {
            erroMs = "This Category Name is already existed";
        } else {
            erroMs = "";

        }
    }

    /**
     * @return the erroMs
     */
    public String getErroMs() {
        return erroMs;
    }

    /**
     * @param erroMs the erroMs to set
     */
    public void setErroMs(String erroMs) {
        this.erroMs = erroMs;
    }
}
